package com.yh.workbill.ui.addworkbill.mvp;

import com.yh.workbill.model.WorkBill;
import com.yh.workbill.model.WorkDevices;

import java.util.List;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class AddWorkBill_Presenter implements LifecycleObserver {
    private AddWorkBill_View view;
    private AddWorkBill_Model model;

    public AddWorkBill_Presenter(AddWorkBill_View view) {
        this.view = view;
        this.model = new AddWorkBill_Model();
    }

    public void addWorkBill(WorkBill workBill) {
        view.showDialog("正在添加数据", false);
        model.addWorkBill(workBill, new AddWorkBill_Model.AddCallBack() {
            @Override
            public void Success(String msg, String company, String unitCompany, WorkBill data) {
                view.addWorkBillCalback(msg, company, unitCompany, data);
                view.hideDialog();
            }

            @Override
            public void fail(String msg) {
                view.addWorkBillFail(msg);
                view.hideDialog();
            }
        });
    }

    public void getDevices() {
        view.showDialog("正在加载数据", true);
        model.getAllDevices(new AddWorkBill_Model.DevicesBack() {
            @Override
            public void Suceess(List<WorkDevices> data) {
                view.hideDialog();
                view.getAllDevices(data);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.showTasot(msg);
            }
        });
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void viewOndestory() {
        if (this.view != null) {
            this.view = null;
        }
    }


}
